Thursday, May 21, 2020

How to Redirect With PHP

Redirection allows you to redirect the client browser to a different URL. You can use it when you're switching domains, changing how your site is structured, or switching to HTTPS.

In this article, I'll show you how to redirect to another page with PHP. I'll explain exactly how PHP redirects work and show you what happens behind the scenes.

Learn PHP With a Free Online Course

If you want to learn PHP, check out our free online course on PHP fundamentals!

How Does the Basic Redirection Work?

Before we dive into the specifics of PHP redirection, let’s quickly understand how exactly HTTP redirection works. Take a look at the following screenshot.

How Redirection Works

Let’s understand what’s going on in the above screenshot:

  • The client browser requests a specific page from the server. In the above example, the client has requested the contents of the index.php file.
  • The server receives the index.php file request and wants to inform the client that it’s no longer available or moved somewhere else, and it should look to a new file instead: new_index.php. The server sends the Location header with a new URL along with the 301 or 302 HTTP code. These are the HTTP codes for redirection.
  • When a client browser encounters the 301 or 302 code, it knows that it has to initiate an another request to a new URL to fetch the content. It initiates a request to fetch the new_index.php file in the above example.
  • Finally, a server sends the contents of the new URL.

So that’s how a basic HTTP redirection works. In the next section, we’ll discuss how the PHP redirection works.

How Redirection Works in PHP?

In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header which performs the actual redirection as we discussed in the previous section.

How to Use Header Function

Let's go through the syntax of the header() function.

  • $header: This is the HTTP header string which you want to use. In our case, we’ll use the Location header for redirection.
  • $replace: It’s an optional parameter which indicates whether the header should replace a previous similar header.
  • $http_response_code: It allows you to send a specific response code.

Now, let’s have a look at the following example to understand how it works altogether.

When the above script is executed, it’ll redirect the client browser to http://www.yoursite.com/new_index.php. In the background, it sends a raw HTTP Location header along with the 302 status code. The 302 status code is used for temporary redirection, but if you want permanent redirection, you can pass the 301 code in the third argument as shown in the following snippet.

The 301 permanent redirect allows you to inform to the search bots that the page is no longer available, and it can be replaced with a new page.

Why Should You Use Die() or Exit() Function After the Header Redirection?

Users with sharp eyes would have noticed that I’ve used the exit() function in the above example. In fact, it’s mandatory that you use either exit() or die() function immediately after the header redirection to stop script execution and avoid any undesired results.

So it’s always recommended practice to use one of these functions after redirection.

The Famous Error: Headers are Already Sent

If you’re an experienced PHP programmer, I’m sure you’ve come across this famous PHP error at some point of time in your day-to-day PHP development. Although for beginners, the situation is really annoying when they encounter this error, since it’s really hard to debug and fix. In most of the cases, they don’t even have a clue that it’s caused by the header redirection.

The rule of thumb is that when you use the header() function in your script, you need to make sure that you don’t send any output before it. Otherwise, PHP will complain with the headers are already sent error. Even if you've sent a single white space before using the header function.

Conclusion

In this post, we discussed one of the important features of PHP programming: redirection. First, we went through the basics of HTTP redirection and then I demonstrated how it works in PHP.

The Best PHP Scripts on CodeCanyon

Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon. With a low-cost one time payment, you can purchase these high-quality WordPress themes and improve your website experience for you and your visitors.

Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.


by Sajal Soni via Envato Tuts+ Code

Pinterest launches Shopping Spotlights to shine more light on shoppable content curated by famous editors, influencers, and publishers

Pinterest, a popular content discovery platform has seen a rise in various online shopping trends amid the coronavirus pandemic. To give a further push, it has come up with an interesting feature called Shopping Spotlights. This feature will allow guest editors, famous influencers, and publishers...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Arooj Ahmed via Digital Information World

How to Change the Font Color in WordPress

Final product image
What You'll Be Creating

Changing the font color in your WordPress site is something you can easily do for a small section of text or a block. Alternatively you can change the color of a specific element or a CSS class throughout your site.

In this quick tutorial, I’ll show you how to quickly change the font color in your WordPress site.

I’ll show you two methods:

  • Changing font color of an individual block.
  • Adding a CSS class to a block for color and then styling the class.

I’ll also give you an overview of changing the font color across your site, using your stylesheet.

If you want to learn how to add completely new fonts to WordPress, or how to change the font size in WordPress you can also learn how here.

How to Change the Font Color of a Section of Text

You can choose to make a section of text or a whole block of text in one of your posts or pages a different color from the rest. You can use this to highlight some text and make it stand out, or to enhance the design of your site.

A word of warning: too many colors will make your site look messy and unprofessional. Make sure you use colors that don’t clash.

Start by finding the section of text whose color you want to change.

paragraph block

Select the text you want to change the color of by double clicking it. Click the arrow icon above the block and select Text Color.

text color selection

Choose the color you want the text to have. The color will change for you in the editing screen.

changing font color

Alternatively, you can use a hex code to select a specific color. This is a good idea if you want to use a color that’s used elsewhere in your site.

Click the Custom color link and either select a color from the picker or type in the hex code.

custom color selection

Repeat this for any other blocks of text or sections of text whose color you want to change. You don’t have to change the color of a whole block—you can just change one word if you want.

Finally, click Update to update your post or Publish to publish it. That will save your changes.

Now your text will be whatever color you gave it:

text with new color in live site

How to Use a CSS Class to Change Font Color

If you want to change a bunch of text blocks so they’re all the same color, the best way to do this is by using a CSS class. This way you know they’ll all be the same color and you won’t have to remember what color you used.

Find your text block again and in the Block pane, open the Advanced tab.

In the Additional CSS class(es) field, type in a CSS class. I’m gong to use highlight-color. I’m deliberately not naming the color in case I decide to change it later on.

adding a CSS class for color to a block

Now click Update or Publish to save your post.

You now have two options for editing the color of blocks using that class:

  • add styling in the Customizer
  • add styling to your theme’s stylesheet

Styling the class in the Customizer is the easiest way to do it, and won’t cause you any problems when you update your theme.

Go to Appearance > Customize in the WordPress admin and click Additional CSS.

customizer

In the Additional CSS pane, type in the CSS to change your font color:

Change your color in that code to whatever color you want to use, using a hex code. It’s a good idea to use a color that’s in use elsewhere in your theme, or one that looks good with your theme while making the text stand out.

CSS color in customizer

Click the Publish button to save your changes.

Now go to your post in your live site and you’ll see that your block with the highlight-color class is a different color than the text around it:

orange text in live site

Now you can use this class for any blocks you want to make that color.

Changing Font Color in Your Theme’s Stylesheet

With the two methods I’ve shown you, you don’t need to edit the files in your theme or have an in-depth understanding of CSS. And they give you a quick way to change the font color in WordPress.

But if you want to edit the color of existing elements in your site (such as all paragraphs or links), or you want to add CSS in the stylesheet instead of in the customizer, you’ll need to edit your theme’s stylesheet.

You can find out more about editing your stylesheet in our guide to adding custom CSS to your WordPress site. If you want to go into more depth, that’s a great place to start.

Conclusion

Changing the font color of text in your WordPress site just takes minutes to do. You can either edit the font color directly, or give it a class and then write CSS for your class.

But however you choose to do this, don’t overdo it. And make sure you use colors that work together well to avoid having a site that looks messy and unprofessional.

Make Your Site Stand Out With a Professional WordPress Theme

Are you in designing a new WordPress website? Our collection of professionally-designed WordPress themes can give your website a beautiful new look and feel, within minutes. 

Here are a few of the best-selling and up-and-coming WordPress themes available on ThemeForest for 2020.



by Rachel McCollin via Envato Tuts+ Code

Top 4 Free Alternatives To Adobe Illustrator That You Can Use Right Now

When it comes to vector graphics, Adobe Illustrator has been the undisputed king for years now. The software has been able to turn so many imaginations into reality, all with the help of mathematical expressions that serves as a foundation to drawing lines, shapes and colors. You can even create an...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Daniyal Malik via Digital Information World

LinkedIn’s State of Sales Report 2020 shines a light on the emerging and enduring trends that will impact sales in the coming days

LinkedIn has released its fourth annual State of Sales report, U.S. edition, and it examines different emerging and enduring trends, and the aftereffects of the coronavirus pandemic on sales. This report also digs into insights about how sales organizations can rebuild their economic stability by...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Arooj Ahmed via Digital Information World

Microsoft Is Adding More Features Including Extension Sync and Sidebar Search To Its Chromium-based Edge

Most of the users wonder how Microsoft Edge went from restoration for the usually-mocked browser, Internet Explorer to the world’s second most significant and well-known desktop browser. That’s because Microsoft switched to Chromium, and currently, as part of the latest build 2020, Microsoft is...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Arooj Ahmed via Digital Information World