Nadia Krawczyk
Nadia Krawczyk

Reputation: 119

Mixing cookies and css with php

So I have really simple css file that is generated based on cookie that is fetched using php. Whole css is also displayed using php so it

<?php header("Content-type: text/css");
$rand = $_COOKIE['randTopic'];

echo '
body {
    margin-top: 50px;
    margin-bottom: 50px;
    background: none;
}

.full {
  background: url("http://loremflickr.com/1920/1080/' . $rand . '") no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  }
';
?>

Loremflickr is just a service that gives me random image from flickr, I can also choose topic what I am doing actually. So the output css looks exactly like this:

body {
    margin-top: 50px;
    margin-bottom: 50px;
    background: none;
}

.full {
  background: url("http://loremflickr.com/1920/1080/wall
") no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  }

As you can see there's a linebreak that I didn't really want, no idea why it's here. I think it could cause the problem, what do you think?

Thank you kindly in advance for any help.

Upvotes: 2

Views: 58

Answers (2)

Elon Than
Elon Than

Reputation: 9765

Quick fix for that is to use trim() function on data from cookie.

But first of all I would check why is there line break. Inspect your code.

Upvotes: 1

Jay Blanchard
Jay Blanchard

Reputation: 34406

trim() the variable in your output -

echo '
....
background: url("http://loremflickr.com/1920/1080/' . trim($rand) . '") no-repeat center center fixed;
....
'; 

Upvotes: 1

Related Questions